package com.common.widget.form;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.InputType;
import android.text.Selection;
import android.text.Spannable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.common.R;


public class FormEditText extends RelativeLayout {

    private String mTagString;
    private String mTextString;
    private String mHintString;
    private Boolean mShowDivider;

    private TextView mTagTextView;
    private EditText mEditTextView;
    private View mDividerView;

    private int mInputType;

    private boolean mIsVerification;

    public FormEditText(Context context) {
        super(context);
        init(null, 0);
    }

    public FormEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs, 0);
    }

    public FormEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs, defStyle);
    }

    public static final int TYPE_NONE = -1;
    public static final int TYPE_MOBILE = TYPE_NONE + 1;
    public static final int TYPE_MESSAGE = TYPE_MOBILE + 1;
    public static final int TYPE_TEL = TYPE_MESSAGE + 1;
    public static final int TYPE_WEB = TYPE_TEL + 1;
    public static final int TYPE_EMAIL = TYPE_WEB + 1;

    private int mActionType;//验证类型
    private void init(AttributeSet attrs, int defStyle) {

        // Load attributes
        final TypedArray a = getContext().obtainStyledAttributes(
                attrs, R.styleable.FormView, defStyle, 0);

        mTagString = a.getString(
                R.styleable.FormView_form_tag);

        mTextString = a.getString(
                R.styleable.FormView_form_text);

        mHintString = a.getString(
                R.styleable.FormView_form_hint);

        mShowDivider = a.getBoolean(
                R.styleable.FormView_form_divider, true);

        mInputType = a.getInt(
                R.styleable.FormView_form_input_type, InputType.TYPE_CLASS_TEXT);
        //是否需要验证
        mIsVerification = a.getBoolean(R.styleable.FormView_form_is_verification,false);

        mActionType = a.getInt(R.styleable.FormView_form_action_type,TYPE_NONE);

        a.recycle();

        LayoutInflater.from(getContext()).inflate(R.layout.view_form_edit_text, this, true);
        mTagTextView = (TextView) findViewById(R.id.form_tag);
        mEditTextView = (EditText) findViewById(R.id.form_edit);
        mDividerView = findViewById(R.id.form_divider);

        if (mTagString != null) {
            mTagTextView.setText(mTagString);
        }

        mTagTextView.setTextColor(getResources().getColor(R.color.form_item_tag_text_color));

        if(!TextUtils.isEmpty(mHintString)){
            mEditTextView.setHint(mHintString);
        }

        if (!TextUtils.isEmpty(mTextString)) {
            mEditTextView.setText(mTextString);
        }

        mEditTextView.setInputType(mInputType);


        mDividerView.setVisibility(mShowDivider ? View.VISIBLE : View.GONE);
    }

    public void setTag(String tag) {
        mTagTextView.setText(tag);
    }

    public void setText(String mTextString) {
        mEditTextView.setText(mTextString);
        CharSequence text = mEditTextView.getText();
        if (text instanceof Spannable) {
            Spannable spanText = (Spannable) text;
            Selection.setSelection(spanText, text.length());
        }
    }

    public void setTextInputType(int inputType){
        mEditTextView.setInputType(inputType);
    }

    public String getText() {
        return  mEditTextView.getText().toString();
    }

    public boolean verification(){
        String textValue = getText();
//        if(!TextUtils.isEmpty(textValue) && mIsVerification){
//            switch(mActionType){
//                case TYPE_NONE://default
//                    break;
//                case TYPE_MOBILE://mobile
//                    if(!CommonUtils.isMobileNO(textValue, true)){
//                        AiKeToast.showToast(getContext(), "手机号不合法!");
//                        return false;
//                    }
//                    break;
//                case TYPE_TEL://tel
//                    if(!CommonUtils.isMobileNO(textValue, false)){
//                        AiKeToast.showToast(getContext(),"电话号码不合法!");
//                        return false;
//                    }
//                    break;
//                case TYPE_WEB://web
//                    break;
//                case TYPE_EMAIL://email
//                    if(!CommonUtils.checkEmail(textValue)){
//                        AiKeToast.showToast(getContext(),"邮箱号码不合法!");
//                        return false;
//                    }
//                    break;
//            }
//        }
        return true;
    }

    public void setHint(String text){
        mEditTextView.setHint(text);
    }

    public EditText getEditTextView(){
        return mEditTextView;
    }
}
